package com.airport.ticket.common;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 所有Controller的父类
 *
 * @author WangshSxt
 */
public class BaseController {
    /**
     * 生成分页对象
     *
     * @param currentPage 当前页
     * @param pageSize    每页有多少条
     * @return
     */
    protected PageInfoUtil proccedPageInfo(String currentPage, String pageSize) {
        /* 分页 默认每页有10条*/
        PageInfoUtil pageInfoUtil = new PageInfoUtil();
        try {
            /* 将字符串转换成整数,有风险,
             * 字符串为a,转换不成不整数
             *  */
            pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
            pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
        } catch (NumberFormatException e) {
        }
        return pageInfoUtil;
    }

    /**
     * 生成一个查询列表公共的搜索条件
     *
     * @param request
     * @return
     */
    protected Map<String, Object> proccedSearch(HttpServletRequest request) {
        Map<String, Object> condMap = new HashMap<String, Object>();
        String keyword = request.getParameter("keyword");
        String status = request.getParameter("status");
        String st = request.getParameter("st");
        String ed = request.getParameter("ed");
        /* request只是可以查询到但是自己并没有存储所以要将查询条件存储到request中 */
        request.setAttribute("keyword", keyword);
        request.setAttribute("status", status);
        request.setAttribute("st", st);
        request.setAttribute("ed", ed);
        /* 关键字 */
        if (keyword == null) {
            keyword = "";
        }
        /* 状态 */
        if (status == null) {
            status = "";
        }
        /* 查询的起始时间和结束时间 */
        Date stDate = null;
        Date edDate = null;
        if (st != null && !"".equalsIgnoreCase(st) && ed != null && !"".equalsIgnoreCase(ed)) {
            stDate = DateUtil.parseToDate(st);
            edDate = DateUtil.parseToDate(ed);
        }
        condMap.put("keyword", keyword);
        condMap.put("status", status);
        condMap.put("stDate", stDate);
        condMap.put("edDate", edDate);
        return condMap;
    }

    /**
     * 生成分页对象
     *
     * @param request
     * @return
     */
    protected PageInfoUtil proccedPageInfo(HttpServletRequest request) {
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        /* 分页 */
        PageInfoUtil pageInfoUtil = new PageInfoUtil();
        try {
            /* 将字符串转换成整数,有风险,
             * 字符串为a,转换不成不整数
             *  */
            pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
            pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
            return pageInfoUtil;
        } catch (NumberFormatException e) {
        }
        return pageInfoUtil;
    }
}
